<?php

namespace app\admin\controller;

use think\Validate;
use think\Db;
use app\models\{PlatClass, Goods, GoodsSpec};

class Shop extends Base
{

    public function goodsrec()
    {
        $list = (new Goods())->where('g.isdel', 2)->alias('g')->join('GoodsSpec gs', 'gs.goodid = g.id')->field('g.*,gs.name,gs.price,gs.stock,gs.unmit,gs.starbuy');
        $data = $this->request->get();
        if (!empty($data['title'])) {
            $list = $list->where('g.title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['classid'])) {
            $list = $list->where('g.classid', $data['classid']);
        }
        if (!empty($data['isSale'])) {
            $list = $list->where('g.isSale', $data['isSale']);
        }
        $list = $list->paginate(10)->each(function ($item) {
            $item['images'] = json_decode($item['images'], true) ?? [];
            $item['class_name'] = (new PlatClass())->where('id', $item['classid'])->value('name') ?? '';
            return $item;
        });
        return view(null, [
            'list' => $list,
            'class' => (new PlatClass())->select()
        ]);
    }
    public function goods()
    {
        $list = (new Goods())->where('g.isdel', 1)->order(['gs.stock' => 'asc'])->alias('g')->join('GoodsSpec gs', 'gs.goodid = g.id')->field('g.*,gs.name,gs.price,gs.stock,gs.unmit,gs.starbuy');
        $data = $this->request->get();
        if (!empty($data['title'])) {
            $list = $list->where('g.title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['classid'])) {
            $list = $list->where('g.classid', $data['classid']);
        }
        if (!empty($data['isSale'])) {
            $list = $list->where('g.isSale', $data['isSale']);
        }
        if (!empty($data['sort'])) {
            $list = $list->where('gs.stock', '<', $data['sort']);
        }
        $list = $list->paginate(10)->each(function ($item) {
            $item['images'] = json_decode($item['images'], true) ?? [];
            $item['class_name'] = (new PlatClass())->where('id', $item['classid'])->value('name') ?? '';
            return $item;
        });
        return view(null, [
            'list' => $list,
            'class' => (new PlatClass())->select()
        ]);
    }


    public function goods_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'classid' => 'require',
                'price' => 'require',
                'unmit' => 'require',
                'starbuy' => 'require',
            ], [
                'title.require' => '商品名称不能为空',
                'classid.require' => '商品分类不能为空',
                'price.require' => '商品价格不能为空',
                'unmit.require' => '商品单位不能为空',
                'starbuy.require' => '商品起购量不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            }
            if (!empty($data['id'])) {
                Db::startTrans();
                try {
                    (new Goods())->allowfield(true)->save($data, ['id' => $data['id']]);
                    (new GoodsSpec())->where('goodid', $data['id'])->update(['name' => $data['title'], 'price' => $data['price'], 'unmit' => $data['unmit'], 'starbuy' => $data['starbuy']]);
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    return json(['status' => 1, 'msg' => '操作失败']);
                }
            } else {
                Db::startTrans();
                try {
                    $goodid = (new Goods())->strict(false)->insertGetId($data);
                    (new GoodsSpec())->save(['name' => $data['title'], 'goodid' => $goodid, 'price' => $data['price'], 'unmit' => $data['unmit'], 'starbuy' => $data['starbuy']]);
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    return json(['status' => 1, 'msg' => '操作失败']);
                }
            }
            return json(['status' => 0, 'code' => 0, 'msg' => '编辑成功']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Goods())->alias('g')->join('GoodsSpec gs', 'gs.goodid = g.id')->field('g.*,gs.name,gs.price,gs.stock,gs.unmit,gs.starbuy')->where('g.id', $id)->find();
            $row['images'] = json_decode($row['images'], true) ?? [];
            return view('', [
                'row' => $row,
                'class' => (new PlatClass())->select()
            ]);
        }
    }

    public function goods_field()
    {
        if ($this->request->isPost()) {
            $id = array_filter(input('id'));
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择项目']);
            }
            $isshow = $this->request->post('status/d', 0);
            $field = $this->request->post('field', '');
            if (empty($isshow) || empty($field)) {
                return json(['status' => 1, 'msg' => '参数值错误']);
            }
            $res = (new Goods())->where(['id' => $id])->update([$field => $isshow]);
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function PlatClass()
    {
        $list = (new PlatClass())->order(['isshow' => 'asc', 'sort' => 'desc', 'id' => 'desc']);
        $data = $this->request->get();
        if (!empty($data['name'])) {
            $list = $list->where('name', 'like', '%' . $data['name'] . '%');
        }
        $list = $list->paginate(10);
        return view(null, [
            'list' => $list
        ]);
    }

    public function platclass_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'name' => 'require',
                'images' => 'require'
            ], [
                'name.require' => '分类名称不能为空',
                'images.require' => '图片路径不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($data['id'])) {
                $res = (new PlatClass())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new PlatClass())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new PlatClass())->where('id', $id)->find();
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function platclass_del()
    {
        if ($this->request->isPost()) {
            $id = array_filter(input('id'));
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择项目']);
            }
            $isshow = $this->request->post('status/d', 1);
            $res = (new PlatClass())->where(['id' => $id])->update(['isshow' => $isshow]);
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }
}
